package staff.util;

import java.io.File;
import java.io.IOException;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

public class XmlUtils {
    /** Name for nodes in XML file with employe data */
    public static final String XML_EMPLOYE_TAG = "employe";
    /** Name for root node in XML file with employe data */
    public static final String XML_ROOT_EMPLOYE_TAG = "employes";

    /** Save DOM XML document to file */
    public static void saveXmlDocumentToFile(Document document, String fileName)
            throws IOException, TransformerConfigurationException,
            TransformerException {
        // prepare DOM document to writing
        Source source = new DOMSource(document);
        // prepare output file
        File file = new File(fileName);
        Result transformResult = new StreamResult(file);
        // write DOM document to file
        Transformer transformer = TransformerFactory.newInstance()
                .newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(source, transformResult);
    }

}
